package com.fsc.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.AnnRepoNews;
import com.fsc.entity.dto.AnnRepoNewsDTO;
import com.fsc.entity.dto.NewsDTO;
import com.fsc.entity.dto.NewsEditDTO;
import com.fsc.entity.dto.NewsUpdateDTO;
import com.fsc.entity.vo.AnnRepoNewsVO;
import com.fsc.service.AnnRepoNewsService;
import com.fsc.util.FileUtil;
import com.fsc.util.OssUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

import static com.fsc._enum.ReturnEnum.SUCCESS;

@RestController
@RequestMapping("/news/")
public class AnnRepoNewsController {
    @Autowired
    private AnnRepoNewsService annRepoNewsService;

    @Resource
    private RedisTemplate<String, byte[]> redisByteTemplate;
    /**
     * 根据 学次id，作者id，隐私，类型，状态获取通知/公告/新闻
     * @param newsDTO 查询信息dto
     * @return 是否成功
     */
    @PostMapping("")
    public ResultWrapper<List<AnnRepoNewsVO>> getNews(@RequestBody NewsDTO newsDTO){
        return annRepoNewsService.getNews(newsDTO);
    }

    @PostMapping("/edit")
    public ResultWrapper<List<AnnRepoNews>> getNews(@RequestBody NewsEditDTO newsEditDTO){
        return annRepoNewsService.getAllNews(newsEditDTO);
    }

    /**
     * 新增通知/公告/信息
     * @param annRepoNewsDTO 新增
     * @return 是否成功
     */
    @PostMapping("add")
    public ResultWrapper<Long> addNews(@RequestBody AnnRepoNewsDTO annRepoNewsDTO){
        AnnRepoNews annRepoNews = AnnRepoNews.builder()
                .content(annRepoNewsDTO.getContent())
                .authorId(annRepoNewsDTO.getAuthorId())
                .schoolId(annRepoNewsDTO.getSchoolId())
                .type(annRepoNewsDTO.getType())
                .title(annRepoNewsDTO.getTitle())
                .privace(annRepoNewsDTO.getPrivace())
                .status(annRepoNewsDTO.getStatus())
                .time(LocalDateTime.now())
                .build();
        annRepoNewsService.save(annRepoNews);
        Long id = annRepoNews.getId();
        return ResultWrapper.success(SUCCESS,id);
    }

    @PostMapping("update")
    public ResultWrapper<String> updateNews(@RequestBody NewsUpdateDTO newsUpdateDTO){
        // 保存更新后的记录到数据库
        annRepoNewsService.update(Wrappers.<AnnRepoNews>lambdaUpdate().eq(AnnRepoNews::getId,newsUpdateDTO.getId())
                .set(AnnRepoNews::getAuthorId,newsUpdateDTO.getAuthorId())
                .set(AnnRepoNews::getSchoolId,newsUpdateDTO.getSchoolId())
                .set(AnnRepoNews::getPrivace,newsUpdateDTO.getPrivace())
                .set(AnnRepoNews::getStatus,newsUpdateDTO.getStatus())
                .set(AnnRepoNews::getType,newsUpdateDTO.getType())
                .set(AnnRepoNews::getTime,LocalDateTime.now())
                .set(AnnRepoNews::getTitle,newsUpdateDTO.getTitle())
                .set(AnnRepoNews::getContent,newsUpdateDTO.getContent()));

        return ResultWrapper.success(SUCCESS);
    }

    /**
     * 根据id删除
     * @param id id
     * @return 是否成功
     */
    @DeleteMapping("del/{id}")
    public ResultWrapper<String> deleteNews(@PathVariable("id") long id){
        return annRepoNewsService.deleteNews(id);
    }

    /**
     * 更改状态
     * @param id id
     * @param status 状态
     * @return 是否成功
     */
    @GetMapping("/update/status")
    public ResultWrapper<String> updataStatus(
            @RequestParam("id") long id,
            @RequestParam("status") int status) {
        return annRepoNewsService.updateStatus(id,status);
    }

    /**
     * 更改隐私状态
     * @param id id
     * @param privace 隐私
     * @return 是否成功
     */
    @GetMapping("/update/privace")
    public ResultWrapper<String> updataPrivace(
            @RequestParam("id") long id,
            @RequestParam("privace") int privace) {
        return annRepoNewsService.updatePrivace(id,privace);
    }

    /**
     * 管理员用上传图片接口
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("upload-pic-user")
    public ResultWrapper<String> uploadPic( @RequestParam("file") MultipartFile file) throws IOException {
        String fileOriginName = file.getOriginalFilename();
        if(!FileUtil.isImage(fileOriginName)){
            return ResultWrapper.fail(ReturnEnum.UPLOAD_FAIL, "请上传正确图片格式");
        }
        String fileName = FileUtil.generateImageName(fileOriginName);
        //将图片存入redis,三分钟有效
//        redisByteTemplate.opsForValue().set(fileName,file.getBytes(),Duration.ofMinutes(30));
//        OssUtil.uploadImage(redisByteTemplate.opsForValue().get(fileName),fileName);
        OssUtil.uploadImage(file.getBytes(),fileName);
        return ResultWrapper.success(ReturnEnum.SUCCESS_UPLOAD_PICTURE,fileName);
    }

}
